package cn.iocoder.yudao.module.biz.controller.admin.invoice.service.impl;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.biz.controller.admin.pay.domain.SanBizPaymentManagement;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.iocoder.yudao.module.biz.controller.admin.invoice.domain.SanBizInvoiceContentDict;
import cn.iocoder.yudao.module.biz.controller.admin.invoice.service.SanBizInvoiceContentDictService;
import cn.iocoder.yudao.module.biz.controller.admin.invoice.mapper.SanBizInvoiceContentDictMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author 王兴鹏
* @description 针对表【san_biz_invoice_content_dict(发票内容字典表)】的数据库操作Service实现
* @createDate 2025-08-25 09:53:46
*/
@Service
public class SanBizInvoiceContentDictServiceImpl extends ServiceImpl<SanBizInvoiceContentDictMapper, SanBizInvoiceContentDict>
    implements SanBizInvoiceContentDictService{

    @Autowired
    private SanBizInvoiceContentDictMapper mapper;

    @Override
    public IPage<SanBizInvoiceContentDict> getBizContentDictPage(SanBizInvoiceContentDict pageReqVO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        PageParam pageParam = new PageParam();
        pageParam.setPageNo(pageReqVO.getPageNo());
        pageParam.setPageSize(pageReqVO.getPageSize());
//        if(pageReqVO.getEffectDateStr() != null && pageReqVO.getEffectDateStr().size()> 1){
//            queryWrapper.ge("effect_date",pageReqVO.getEffectDateStr().get(0));
//            queryWrapper.le("effect_date",pageReqVO.getEffectDateStr().get(1));
//        }
        if(StringUtils.isNotEmpty( pageReqVO.getName())){
            queryWrapper.like("name", pageReqVO.getName());
        }
        IPage<SanBizInvoiceContentDict> mpPage = MyBatisUtils.buildPage(pageParam);
        return mapper.selectPage(mpPage, queryWrapper);
    }

    @Override
    @Transactional
    public String importList(List<SanBizInvoiceContentDict> list, Boolean updateSupport) {
        if (list == null || list.size() == 0)
        {
            throw new ServiceException(4041,"导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
//        String password = configService.selectConfigByKey("sys.user.initPassword");
        for (SanBizInvoiceContentDict user : list)
        {
            try
            {
                // 验证是否存在
//                QueryWrapper<SanBizPaymentManagement> queryWrapper = new QueryWrapper<>();
//                queryWrapper.eq("contract_code",user.getContractCode());
                SanBizPaymentManagement u = null; // getOne(queryWrapper);
                if (u == null)
                {
//                    BeanValidators.validateWithException(validator, user);
//                    user.setPassword(SecurityUtils.encryptPassword(password));
                    user.setCreator(SecurityFrameworkUtils.getLoginUser().getId()+"");
                    this.save(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、 " + user.getName() + " 导入成功");
                }
                else if (updateSupport)
                {
//                    BeanValidators.validateWithException(validator, user);
                    user.setUpdater(SecurityFrameworkUtils.getLoginUser().getId()+"");
                    user.setId(u.getId());
                    this.updateById(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、合同 " + user.getName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、合同 " + user.getName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、合同 " + user.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(4041,failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}




